<?php
namespace Resource\Service;
use Think\Model;

/**
 * 公共配置
 * Class ItemService
 * @package Resource\Service
 */
class CommonSetService extends Model{
    protected $trueTableName = 'wz_common_set';

    /**
     * @param $params
     * @return array|bool
     */
    public function addCommonSet($params) {
        $params['create_time'] = time();
        $params['update_time'] = time();
        $id = D('Resource/CommonSet')->addCommonSet($params);
        if($id) {
            return ['id'=>$id];
        } else {
            return false;
        }
    }

    /**
     * @param $params
     * @param string $fields
     * @return mixed
     */
    public function getCommonSetInfo($params, $fields='') {
        $info = D('Resource/CommonSet')->getCommonSetInfo($params,$fields);
        return $info;
    }

    /**
     * @param $params
     * @return array
     */
    public function searchCommonSetList($params) {
        $page = 1;
        $page_size = 15;
        isset($params['page']) && $page = $params['page'];
        isset($params['page_size']) && $page_size = $params['page_size'];

        $data = [];
        $data['status'] = ['in',[-1,0,1]];
        isset($params['title']) && $data['title'] = ['like', "%{$params['title']}%"];
        isset($params['type']) && $data['type'] =$params['type'];

        //查询项目总数
        $total = D('Resource/CommonSet')->getCommonSetCount($data);

        //计算总页数
        $total_page = ceil($total / $page_size);
        if($page < 1) {
            $page = 1;
        }
        if($page > $total_page) {
            $page = $total_page;
        }

        //查询项目列表
        $itemList = D('Resource/CommonSet')->getCommonSetList($data, $page, $page_size);

        $result = [];
        $result['page'] = $page;
        $result['page_size'] = $page_size;
        $result['total'] = $total;
        $result['total_page'] = $total_page;
        $result['dataList'] = $itemList;
        return $result;
    }

    /**
     * @param $params
     * @return mixed
     */
    public function updateCommonSetInfo($params) {
        return D('Resource/CommonSet')->updateCommonSetInfo($params);
    }

    /**
     * @param $params
     * @return mixed
     */
    public function getAllCommonSetList($params){
        $dataList = D('Resource/CommonSet')->getAllCommonSetList($params);
        return $dataList;
    }
}